package client

import (
	"addsvc/internal/middleware"
	"addsvc/internal/utils/method"
	"time"

	kitEndpoint "github.com/go-kit/kit/endpoint"
	"github.com/go-kit/kit/sd"
	"github.com/go-kit/kit/sd/etcdv3"
	"github.com/go-kit/kit/sd/lb"
	"github.com/go-kit/log"
	semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
	"go.opentelemetry.io/otel/trace"
)

func makeEndpoint(
	prefix string,
	client etcdv3.Client,
	method method.Method,
	logger log.Logger,
	tracer trace.Tracer,
	f sd.Factory,
) kitEndpoint.Endpoint {
	// ectd 实例
	instancer, _ := etcdv3.NewInstancer(client, prefix, logger)
	endpointer := sd.NewEndpointer(instancer, f, logger)
	// 负载均衡
	balancer := lb.NewRoundRobin(endpointer)
	point := lb.Retry(3, 3*time.Second, balancer)
	point = middleware.Chains(method, logger)(point)
	// 链路追踪
	point = middleware.TraceEndpoint(
		tracer,
		string(method),
		trace.WithSpanKind(trace.SpanKindClient),
		trace.WithAttributes(semconv.NetworkProtocolName("grpc")),
	)(point)

	return point
}
